<?php 
//if (! defined('BAEPATH')) exit('No direct script access allowed');           
class home extends MY_Controller {

	//constructor
	 function __construct()
	 {                                                                   
		parent::__construct();              
		$this->load->helper('form'); 
        //$this->content_view='';

        $this->view_data['selectedmenu']='user';
        $this->content_view='v_home';
 	 }

 	public function index()
	{       
        $this->ShowUserProfile();                                           
  	}
    function ShowDepartment()
    {
        $depart = Department::all();
         echo '<pre>';
        print_r ($depart ) ;
        echo '<br/>';
        
    }
    
    function ajaxSaveProfile()
    {
        
        $this->ClearContent();
        $data = array('name'=>$_POST['name'],
                      'email'=>$_POST['email'],
                      'mobilephone'=>$_POST['mobile'],
                      'lang'=>$_POST['lang'],
                      'department_id'=>$_POST['dept']);
                      
       if ($this->user->SaveProfile($data)) 
            $msg = getLangText('Changes saved',false);
        else
            $msg = getLangText('Failed to save changes',false);   
        //echo $msg;
        echo json_encode(array('status'=>$msg)); 
    }
    
    function ajaxChangePassword()
    {
        $this->ClearContent();
        if (!isset($_POST['oldpsw']) || !isset($_POST['newpsw']))
            die('no password detected');
            
        $old= $_POST['oldpsw'];
        $new= $_POST['newpsw'];

                                                                           
       if (!$this->user->ValidatePassword($old)) 
            echo json_encode(array('status'=> getLangText('Incorrect current password',false))); 
       else if ($this->user->changePassword($old,$new))
            echo json_encode(array('status'=>getLangText('Password has been changed',false))); 
        else
            echo json_encode(array('status'=>getLangText('Failed to change passord',false)));                        
    }
    
    function LoadUser()
    {
        $users = User::all();
        /*
        foreach($users as $user)
        {
            echo 'username:'.$user->username.'<br/>';
            echo 'department:'.$user->department->depart_name.'<br/>';
            echo '<hr/>';
        }
        echo '<br/>';
        */
        $this->view_data['users']=$users;
        $this->view_data['selectedmenu']='user';
        
    }
    public function ShowUserProfile()
    {
                                                                
        if($this->user)
        {                                                       
            $depart = Department::all();            
            $this->view_data['user'] = $this->user;      
            $this->view_data['departments'] = $depart;      
        }
        else
            echo 'cannot file user profile for user '.$this->user->user_id;        
    }
  
    public function Logout()
    {
        User::Logout();
        echo 'loggout';
        redirect(base_url());
    }
}/* End of class */

?>